//
//  KABTicCheResultVC.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/16.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABTicCheResultVC.h"
#import "KABTicCheResultTCell.h"
#import "UIButton+EdgeInsets.h"
#import "ApiManager.h"
#import "MBProgressHUD+XDPrompt.h"
#import "XDAFNetworking.h"
#import "GlobalHeader.h"
#import "KABModel.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface KABTicCheResultVC ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>
@property (strong, nonatomic) IBOutlet UIView *headerBgView;
@property (weak, nonatomic) IBOutlet UIButton *checkResultBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) KABExhOrderCheckResult *order;

@end

static NSString *const kReuseCellId = @"KABTicCheResultTCell";

@implementation KABTicCheResultVC

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (void)initNavigationBar {
    self.navigationItem.title = @"门票校验结果";
}

- (void)initSubViews {
    [self.checkResultBtn layoutButtonWithEdgeInsetsStyle:ButtonEdgeInsetsStyleImageTop imageTitlespace:8.0];
    CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat height = width * 153.0 / 375.0;
    self.headerBgView.frame = CGRectMake(0, 0, width, height);
//    self.tableView.tableHeaderView = self.headerBgView;
    
    UINib *nib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kReuseCellId];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 45.0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.hidden = YES;
    
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    
    // 请求校验
    [self requestExhOrderCheck:self.jump_bar_code];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (self.order.err_code == 200) ? 9 : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    KABTicCheResultTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
    [cell configViewWithOrderDetail:self.order.result index:indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5.9;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ;
}

#pragma mark - DZNEmptyDataSetSource
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *qrstr = [NSString stringWithFormat:@"二维码：%@", self.jump_bar_code];
    NSString *str = self.order.msg ? self.order.msg : qrstr;
    return [[NSAttributedString alloc] initWithString:str];
}

#pragma mark - event response
#pragma mark - private methods
- (void)refreshWholeView {
//    self.tableView.hidden = NO;
    self.tableView.tableHeaderView = self.headerBgView;
    self.checkResultBtn.selected = self.order.result.check_result;
    
    [self.tableView reloadData];
}

#pragma mark - network request
- (void)requestExhOrderCheck:(NSString *)numberCode {
    // 请求购物车必须要登录
    if (![KABModel sharedInstance].isLogin) {
        [[NSNotificationCenter defaultCenter] postNotificationName:KABGlobalNotiUserTokenInvalid object:nil];
        return;
    }
    
    NSArray *array = [numberCode componentsSeparatedByString:@"|"];
    if (array.count != 2) {
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"无效二维码!"];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD xd_showHUDAddedTo:self.view animated:YES];
    
    NSDictionary *pars = @{@"user_id" : @([KABModel sharedInstance].userInfo.uid),
                           @"access_token" : [KABModel sharedInstance].userInfo.access_token,
                           @"serial_number" : array.firstObject,
                           @"check_code" : array.lastObject};
    
    [[XDAFNetworking sharedInstance] GET:[ApiManager apiExhibitionOrderCheck] parameters:pars progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"校验门票返回：%@", responseObject);
        [hud hideAnimated:YES];
        self.order = [KABExhOrderCheckResult yy_modelWithDictionary:responseObject];
        if ([responseObject[KABGlobalJsonKeyErrCode] integerValue] == 200) {
            ;
        } else {
            [MBProgressHUD showPromptWithAutoHideInView:self.view message:responseObject[KABGlobalJsonKeyMsg]];
        }
        [self refreshWholeView];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"校验门票：%@", error);
        [hud hideAnimated:NO];
        [MBProgressHUD showPromptWithAutoHideInView:self.view message:@"网络请求失败"];
    }];
}

#pragma mark - getters and setters


@end
